package com.app.service.impl;

import com.app.mapper.CusEducationMapper;
import com.app.mapper.LookMeMapper;
import com.app.model.CusEducationModel;
import com.app.model.EnterpriseModel;
import com.app.model.LookMeModel;
import com.app.model.SkillTrainingModel;
import com.app.service.CusEducationService;
import com.app.service.LookMeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description:收藏，关注，谁看过我
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class LookMeServiceImpl implements LookMeService {
    @Autowired
    LookMeMapper lookMeMapper;

    @Override
    public LookMeModel findRidAndPid(int pid, int rid, int eid, int cid, int userType, int lookMeType) {
        QueryWrapper<LookMeModel> queryWrapper = new QueryWrapper<>();
        if (pid > 0) {
            queryWrapper.select().eq("pid", pid);
        }
        if (rid > 0) {
            queryWrapper.select().eq("rid", rid);
        }
        if (eid > 0) {
            queryWrapper.select().eq("eid", eid);
        }
        if (cid > 0) {
            queryWrapper.select().eq("cid", cid);
        }
        if (userType > 0) {
            queryWrapper.select().eq("user_type", userType);
        }
        if (lookMeType > 0) {
            queryWrapper.select().eq("look_me_type", lookMeType);
        }

        List<LookMeModel> userList = lookMeMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    @Override
    public Page<LookMeModel> findLookMeList(int userType, int lookMeType, int cid, int eid, int page, int limit) {
        QueryWrapper<LookMeModel> queryWrapper = new QueryWrapper<>();
        if (eid > 0) {
            queryWrapper.select().eq("eid", eid);
        }
        if (cid > 0) {
            queryWrapper.select().eq("cid", cid);
        }
        Page<LookMeModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        queryWrapper.select().eq("user_type", userType).eq("look_me_type", lookMeType).orderByDesc("id");
        Page<LookMeModel> userList = lookMeMapper.selectPage(pages, queryWrapper);
       return userList;
    }

    @Override
    public int findLookMeCount(int userType, int lookMeType, int cid, int eid) {
        QueryWrapper<LookMeModel> queryWrapper = new QueryWrapper<>();
        if (eid > 0) {
            queryWrapper.select().eq("eid", eid);
        }
        if (cid > 0) {
            queryWrapper.select().eq("cid", cid);
        }

        queryWrapper.select().eq("user_type", userType).eq("look_me_type", lookMeType);
        int count = lookMeMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public int insert(LookMeModel model) {
        return lookMeMapper.insert(model);
    }

    @Override
    public void delete(LookMeModel model) {
        QueryWrapper<LookMeModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("rid", model.getRid()).eq("look_me_type", model.getLookMeType()).eq("user_type", model.getUserType()).eq("cid", model.getCid());
        lookMeMapper.delete(queryWrapper);
    }

    @Override
    public int selectAllCount(int eid, int cid, int userType, int lookMeType) {
        QueryWrapper<LookMeModel> queryWrapper = new QueryWrapper<>();
        if (eid > 0) {
            queryWrapper.select().eq("eid", eid);
        }
        if (cid > 0) {
            queryWrapper.select().eq("cid", cid);
        }
        if (userType > 0) {
            queryWrapper.select().eq("user_type", userType);
        }
        if (lookMeType > 0) {
            queryWrapper.select().eq("look_me_type", lookMeType);
        }
        int count = lookMeMapper.selectCount(queryWrapper);
        if (count < 0) {
            return Integer.parseInt(null);
        }
        return count;

    }

    @Override
    public int getCountByPid(int pid) {
        QueryWrapper<LookMeModel> queryWrapper = new QueryWrapper<>();
        if (pid > 0) {
            queryWrapper.select().eq("pid", pid);
        }
        int count = lookMeMapper.selectCount(queryWrapper);
        if (count < 0) {
            return Integer.parseInt(null);
        }
        return count;
    }
}
